//
//  TodayViewController.m
//  todayTool
//
//  Created by 魏延龙 on 2020/9/21.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import <Masonry/Masonry.h>

@interface TodayViewController () <NCWidgetProviding, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataSource;

@property BOOL expaned;

@end

@implementation TodayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //设置widget可扩展
    if (@available(iOS 10.0, *)) {
        self.extensionContext.widgetLargestAvailableDisplayMode = YES;
    } else {
        // Fallback on earlier versions
    }
    self.dataSource = @[
        @"webView URL 拦截",
        @"MD5",
        @"VFL 布局",
        @"下拉放大效果",
        @"scrollView 嵌套 tableView",
        @"修改状态栏 和导航栏颜色"];
    [self setupViews];
}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
    // Perform any setup necessary in order to update the view.

    // 官方建议在这获取数据
    completionHandler(NCUpdateResultNewData);
}

//紧凑,扩展的显示模式发生改变时回调
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize API_AVAILABLE(ios(10.0))
{
    NSLog(@"widgetActiveDisplayModeDidChange:%d,height = %.2f, width = %.2f", (int)activeDisplayMode, maxSize.height, maxSize.width);

    if (activeDisplayMode == NCWidgetDisplayModeCompact) {
        //紧凑时更改size至最大值
        self.preferredContentSize = CGSizeMake(maxSize.width, 44 * 2);
        self.expaned = NO;
    } else {
        //扩展时,更改size.height至300
        self.preferredContentSize = CGSizeMake(maxSize.width, 44 * 5);
        self.expaned = YES;
    }
    [self.tableView reloadData];
}

- (void)setupViews {
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.expaned ? 5 : 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSURL *url = [NSURL URLWithString:@"todaytool://wkwebview"];
    [self.extensionContext openURL:url completionHandler:^(BOOL success) {
        NSLog(@"open status: %d",success);
    }];
}

/*
 与主APP 数据通信
 参考 https://www.jianshu.com/p/062d33bcf38f
 因开发者账号原因未测试
 [[[NSUserDefaults alloc] initWithSuiteName:@"group.com.mrgcy.appExtension"] setValue:@"存储的值" forKey:@"key"];
 [[[NSUserDefaults alloc] initWithSuiteName:@"group.com.mrgcy.appExtension"] objectForKey:@"key"];
 1. 配置 App Groups
 2. 其中@"group.com.mrgcy.appExtension"这个就是你创建组的时候添加的
 */

/*
 注意：
 1.扩展的版本号不应该大于主app版本号
 2.扩展是独立的进程，要在开发者中心申请bundle id
 */

@end
